import express from "express";
import cors from "cors";
import path from "node:path";
import fs from "node:fs";

const app = express();
app.use(cors());
app.use(express.json());
app.post("/download", (req, res) => {
  const filename = req.body.filename;
  //process.cwd() 根目录
  const filePath = path.join(process.cwd(), "static", filename);
  const content = fs.readFileSync(filePath); //不能加utf-8
  //两个响应头 Content-Type octet-stream（二进制流）
  //Content-Disposition attachment;filename=xxx.pdf   我们在网页中打开图片直接预览不是下载，默认inline 内联模式 attachment把文件当做一个附件进行下载
  res.setHeader("Content-Type", "application/octet-stream");
  res.setHeader("Content-Disposition", `attachment;filename=${filename}`);
  res.send(content);
});
app.listen(3001, () => {
  console.log("Server is running on port 3001");
});
